/*
 * Created on 16.04.2007
 */
package com.hieu.sms.utils;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.transaction.Status;
import javax.transaction.UserTransaction;


public final class ConnectionSourceUtil {
    public final static void closeConnection(Connection connection) {
	if (connection != null) {
	    try {
		connection.close();
	    } catch (Exception e) {
	    }
	}
    }

    public final static void closeStatement(Statement stmt) {
	if (stmt != null) {
	    try {
		stmt.close();
	    } catch (Exception e) {
	    }
	}
    }

    public final static void closePreparedStatement(PreparedStatement pstmt) {
	closeStatement(pstmt);
    }

    public final static void closeCallableStatement(CallableStatement cstmt) {
	closeStatement(cstmt);
    }

    public final static void closeResultSet(ResultSet rs) {
	if (rs != null) {
	    try {
		rs.close();
	    } catch (Exception e) {
	    }
	}
    }

    public final static void rollback(UserTransaction trn) {
	try {
	    if (trn.getStatus() != Status.STATUS_NO_TRANSACTION) {
		trn.rollback();
	    }
	} catch (Exception e) {
	}
    }

    public final static void rollback(Connection connection) {
	try {
	    if (connection != null) {
		connection.rollback();
	    }
	} catch (Exception e) {
	}
    }

    public final static void setAutoCommit(Connection connection,
	    boolean autoCommit) {
	if (connection != null) {
	    try {
		connection.setAutoCommit(autoCommit);
	    } catch (Exception e) {
	    }
	}
    }
}
